/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dbhandler;

import evthandler.EventMessage;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author Casper, Dennis, Jackie, Jakob
 * @version 1.00
 */
public class Company {

    private int companyNo;
    private String companyName;
    private String companyEmail;
    private String companyPhone;
    private Address address;

    /**
     *
     */
    public Company() {
    }

    /**
     *
     * @param companyNo
     */
    public Company(int companyNo) {
        this.companyNo = companyNo;
        int addressNo = 0;
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT * FROM company WHERE companyNo=" + companyNo);
            while (rs.next()) {
                companyName = rs.getString("companyName");
                companyEmail = rs.getString("companyEmail");
                companyPhone = rs.getString("companyPhone");
                addressNo = rs.getInt("addressNo");
            }
            address = new Address(addressNo);
        } catch (SQLException ex) {
            new EventMessage("SQL error in Company", "There was an error performing the sql inquery\n").showException(ex);
        }
    }

    /**
     *
     * @return
     */
    public ArrayList<Company> getCompanies() {
        ArrayList<Integer> companyNos = new ArrayList<Integer>();
        ArrayList<Company> companies = new ArrayList<Company>();
        try {
            ResultSet rs = DbCon.getInstance().executeQuery("SELECT companyNo FROM company ORDER BY companyName");
            while (rs.next()) {
                companyNos.add(rs.getInt("companyNo"));
            }
            for (int i = 0; i < companyNos.size(); i++) {
                companies.add(new Company(companyNos.get(i)));
            }
        } catch (SQLException ex) {
            new EventMessage("SQL error in Company.getCompanies()", "There was an error performing the sql inquery\n").showException(ex);
        }
        return companies;
    }

    /**
     *
     * @return
     */
    public int getCompanyNo() {
        return companyNo;
    }

    /**
     *
     * @param companyName
     */
    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }

    /**
     *
     * @param companyEmail
     */
    public void setCompanyEmail(String companyEmail) {
        this.companyEmail = companyEmail;
    }

    /**
     *
     * @param companyPhone
     */
    public void setCompanyPhone(String companyPhone) {
        this.companyPhone = companyPhone;
    }

    /**
     *
     * @param address
     */
    public void setAddress(Address address) {
        this.address = address;
    }

    /**
     *
     */
    public void insert() {
        try {
            DbCon.getInstance().executeUpdate("INSERT INTO company (companyName, companyEmail, companyPhone, addressNo) VALUES ('" + companyName + "', '" + companyEmail + "', '" + companyPhone + "', " + address.getAddressNo() + ")");


        } catch (SQLException ex) {
            new EventMessage("SQL error in Company.insert()", "There was an error performing the sql inquery\n").showException(ex);

        }
    }

    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return companyName;
    }
}
